package org.zjx.command.game;

import org.zjx.command.Command;
import org.zjx.command.CommandFactory;
import org.zjx.command.CompositeCommand;
import org.zjx.command.CompositeCommandBuilder;
import org.zjx.command.basic.*;
import org.zjx.command.composite.FindImageCommand;
import org.zjx.command.composite.IdentifyFontCommand;
import org.zjx.command.login.LoginCommandConfig;
import org.zjx.core.IWindow;
import org.zjx.entity.ParagraphBean;
import org.zjx.enums.FontAttribute;
import org.zjx.state.StateManager;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Collections;
import java.util.List;

public class DialogueNpcCommand extends CompositeCommand {
    public static final Rectangle closeRectangle = LoginCommandConfig.createRect(134 + 524, 261 + 33, 134 + 539, 261 + 48);
    public static final BufferedImage closeImage = LoginCommandConfig.createImage("iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADAUlEQVR4XlWUS0wTURSGZ61oaact7bRU+6CvoWUo5ZUqoqSkio9WCCAgRUIUEjA+UDGGAAIBGqMskIyGRWPUNCHRJhKdjUkXLprogoULFy667MIFCxddmPzOPQ0SFyf3zs2c7/7zn3OGU+RxKFssbuGDPIXU7S6M90YQbfWgvdEFp82AoFtAc8CGrjYR08lTeLMygMxIJZSNEXC51zPIpW/h3Ysp3El24ETYDZtZB11lRTk0hw9WzSGERRvuJk9j+8lVgnC5zAw+bk1j8loMtW4rdNoKHBcENIUl2psMGpiNlWiSJAz3X4HDVYOgR8DDsShBuPzOOlIP+hESj4PXHoHdIiCdTqNYLGJ5bh68Xk+Ak5FmOtv9votISwiJjiDSi33gPm8vYrC7g5KFKh3qQ356sfSnhMLPgvosqgAt+vt6UCqV6HxlfhbRiB9zEzFwm6mbCEtu6HVHIZh4CBYzlucX6EWWEO86j5ZQiG7eh3b3XIDrWBWp4B7PJGG3mVSAhgD2Y2b4vS4oOwolpF+nkclkaM/Omhsl1ZdKqk7Yb/kf4KyuhuR3wOu248boGEq/y5L3kyW/H+YqLUyqJ6xSDMIt3CsDrGpycmAI9WItfE4HLsXPYm9vj5KLv4rojLWrBpsPAAKvXqgvK3CosvUGI+QNWf3mC2htaICiKCgUCsjn8+TF+vo6BOEAwMrLeoJ7tjQBSXTAwGuwurpKZrHI5XLojcfh87ggyzKpuX/vDrxOOwF0PE+dyWVfLWCwpwMWM48z0Tbkv+Xx5NkaJUp+O+oCXtovzz9GNptFW4tEncmqcLO/FdyPLzKYipMtAVJhsVZTNRjQp97mc1UTpC7gUf0RyWCTuQoXo/XYmk0wQBpflU2sPBpDpEmkahiMxn+QgMeJoFiDOp8XDqtV7Uye5mVpurs8TEzBPuTpwgQS51rhq7ERaL83yvuj8DgtuNwZRupugiaXTTIpoMizVcantyt4rnbn5Ggc14di5E+ytxPjwzGsPUzi/csHNL1sitkv4C9+dhVDKfFT5AAAAABJRU5ErkJggg==");

    public static final Rectangle nameRectangle = LoginCommandConfig.createRect(134 + 47, 261 + 1, 134 + 141, 261 + 27);
    public static final List<Color> nameColors = Collections.singletonList((new Color(255, 255, 255)));

    public static final Rectangle contentRectangle = LoginCommandConfig.createRect(134 + 11, 261 + 36, 134 + 501, 261 + 183);
    public static final List<Color> contentColors = Collections.singletonList((new Color(238, 0, 0)));

    private final List<String> dialogues;

    public DialogueNpcCommand(IWindow targetWindow, StateManager stateManager, List<String> dialogues) {
        super(targetWindow, stateManager);
        this.dialogues = dialogues;
    }

    @Override
    protected void initializeSubCommands() {
        addWithResult(
                new ScreenshotCommand(targetWindow),
                "screenshot",
                cmd -> ((ScreenshotCommand) cmd).getScreenshot()
        );

        addDynamic(context -> {
            IdentifyFontCommand identifyFontCommand = new IdentifyFontCommand(targetWindow, stateManager, get("screenshot"), contentRectangle, contentColors, FontAttribute.NPC, 0);
            identifyFontCommand.execute();
            List<ParagraphBean.Word> wordBeans = identifyFontCommand.getResult();
            if (wordBeans != null && !wordBeans.isEmpty()) {
                for (ParagraphBean.Word word : wordBeans) {
                    for (String dialogue : dialogues) {
                        if (word.getName().equals(dialogue)) {
                            CommandFactory.gameClick(targetWindow, stateManager, true,(int) word.getRectangle().getCenterX(), (int) word.getRectangle().getCenterY());
                            return new DelayCommand(targetWindow, 1000);
                        }
                    }
                }
            }
            close(targetWindow, stateManager, get("screenshot"));
            return new DelayCommand(targetWindow, 0);
        });
    }


    public static void close(IWindow targetWindow, StateManager stateManager, BufferedImage screenImage) {
        if (!isOpen(screenImage)) return;
        GameMoveCommand gameMoveCommand = new GameMoveCommand(targetWindow, stateManager, closeRectangle);
        gameMoveCommand.execute();
        CommandFactory.waitDirectly(targetWindow, 30);
        ClickCommand clickCommand = new ClickCommand(targetWindow, 1, 1);
        clickCommand.execute();
        CommandFactory.waitDirectly(targetWindow, 300);
    }

    public static boolean isOpen(BufferedImage screenImage) {
        Point point = new Point(134, 340);
        Color color = new Color(240, 228, 168);
        return screenImage.getRGB(point.x, point.y) == color.getRGB();
    }
}
